package com.wendy.authentication;

import com.wendy.mapper.MenuMapper;
import com.wendy.mapper.ResourceMapper;
import com.wendy.model.Menu;
import com.wendy.model.Resource;
import com.wendy.utils.Constants;
import com.wendy.utils.RequestUriHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author zhuwending
 */
@Component
@Slf4j
public class UserFilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {

    @Autowired
    private UserInvocationSecurityMetadataSourceService securityMetadataSource;
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    public void setAccessDecisionManager(UserAccessDecisionManager accessDecisionManager) {
        super.setAccessDecisionManager(accessDecisionManager);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        FilterInvocation invocation = new FilterInvocation(request, response, filterChain);
        insertMenuOrResource(invocation);
        invoke(invocation);
    }

    private void invoke(FilterInvocation filterInvocation) throws IOException, ServletException {
        //filterInvocation里面有一个被拦截的url
        //里面调用UserInvocationSecurityMetadataSource的getAttributes(Object object)这个方法获取fi对应的所有权限
        //再调用UserAccessDecisionManager的decide方法来校验用户的权限是否足够
        InterceptorStatusToken token = super.beforeInvocation(filterInvocation);
        try {
            //执行下一个拦截器
            filterInvocation.getChain().doFilter(filterInvocation.getRequest(), filterInvocation.getResponse());
        } finally {
            super.afterInvocation(token, null);
        }
    }

    private void insertMenuOrResource(FilterInvocation invocation) {
        String uri = invocation.getRequestUrl();
        uri = RequestUriHandler.handlerUri(uri);
        if (RequestUriHandler.needPermissions(uri)) {
            return;
        }
        Resource resource = resourceMapper.findResourceByUrl(uri);
        Menu menu = menuMapper.selectMenuByPath(uri);
        if (resource != null || menu != null) {
            return;
        }
        if (uri.startsWith(Constants.MENU_PATH)) {
            menu = new Menu();
            menu.setPath(uri);
            menuMapper.insertSelective(menu);
        } else {
            resource = new Resource();
            resource.setPath(uri);
            resourceMapper.insertSelective(resource);
        }
    }

    @Override
    public Class<?> getSecureObjectClass() {
        return FilterInvocation.class;
    }

    @Override
    public SecurityMetadataSource obtainSecurityMetadataSource() {
        return this.securityMetadataSource;
    }
}
